#include "videoplay.h"
#include "decoder.h"

#include <QPainter>
#include <QDebug>
#include <QMessageBox>

VideoPlay::VideoPlay(QWidget *parent): QWidget(parent)
{
    m_Decoder = new Decoder(this);
    connect(m_Decoder, &Decoder::sendFrame, this, &VideoPlay::recvFrame);


    QObject::connect(m_Decoder, &Decoder::finished, this, [this](){
        if(m_Decoder->getRunState() != 0) {
            QMessageBox::warning(this, "标题", "打开url 失败");
        }else {
            QMessageBox::information(this, "标题", "播放结束");
        }
    });
}

void VideoPlay::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    painter.drawImage(rect(),m_image); //画出图像
}

void VideoPlay::recvFrame(QImage image)
{
    m_image = std::move(image);
    update();
}

VideoPlay::~VideoPlay()
{
    m_Decoder->terminate();
}


bool VideoPlay::openUrl(QString url)
{
    bool sta = url.startsWith("rtsp://");
    if(! sta) {
        qDebug() << "url.startsWith(\"rtsp://\") is false ";
        return false;
    }
    m_Decoder->setUrl(url);
    m_Decoder->start();
}
